/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servidor;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author imarban
 */
public class Servidor extends Thread
{
    private static final int PUERTO = 6000;
    ServerSocket serverSocket = null;
    
    public Servidor()
    {   try {
            serverSocket = new ServerSocket(PUERTO);
            System.out.println("Funcionando");
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
        }       
    start();
    }


    @Override
    public void run() {
         try {
            while(true){
                System.out.println("Listo para recibir conexiones");
                Socket socketCliente = serverSocket.accept();
                ObjectOutputStream oos = new ObjectOutputStream(socketCliente.getOutputStream());
                oos.flush();
                ObjectInputStream ois = new ObjectInputStream(socketCliente.getInputStream());
                System.out.println("Cliente conectado de " + socketCliente.getInetAddress());
                switch (ois.readInt())
                {
                    case Constantes.ALUMNO:
                        System.out.println("Recibi un codigo de entidad ALUMNO");
                        ServerAlumno serverAlumno = new ServerAlumno(socketCliente, ois, oos);
                        break;
                    case Constantes.CURSO:
                        ServerCurso serverCurso = new ServerCurso(socketCliente, ois, oos);
                        break;
                    case Constantes.ASIGNACION:
                        ServerAsignacion serverAsignacion = new ServerAsignacion(socketCliente, ois, oos);
                        break;
                }
                    
                    
            }
         } catch(IOException ioe){
             System.out.println(ioe.getMessage());
         }
        
    }
    
    public static void main(String[] args) {
        new Servidor();
    }
    
    
}
